package com.weseal.controller.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author weseal
 * @title
 * @description
 * @usage
 * @copyright Copyright 2014  rrmj Corporation. All rights reserved.
 * @company 上海众多美网络科技有限公司
 * @create 2017/9/9
 */
public class BaseController {

	@Autowired
	public HttpSession session;

	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected HttpServletResponse response;

//	@Autowired
//	protected FunctionInfoService functionInfoService;

	public void addCookie(String key, String value) {
		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(60 * 60 * 24 * 7);
		cookie.setPath("/");
		response.addCookie(cookie);
	}


	public void removeCookie(String key) {
		Cookie cookie = new Cookie(key, null);
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);
	}


	/**
	 * 获取当前用户
	 *
	 * @return
	 */
//	public MasterInfoVo getMaster() {
//		org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
//		return (MasterInfoVo) subject.getPrincipal();
//	}

	//@ModelAttribute
//	protected void initFunction(ModelMap model) {
//		Set<FunctionInfoVo> functionInfoVos = (Set<FunctionInfoVo>) session.getAttribute(CommonUtils.CURRENT_ROLE);
//		if (org.apache.commons.collections.CollectionUtils.isEmpty(functionInfoVos)) {
//			functionInfoVos = functionInfoService.queryAll();
//			session.setAttribute(CommonUtils.CURRENT_ROLE, functionInfoVos);
//		}
//		model.put("functionInfos", functionInfoVos);
//	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
